var miniYUI = {
    dom: {  /*--dom{{--*/
        hasClass: function(el, className) {
            return className && (' ' + el.className + ' ').indexOf(' ' + className + ' ') > -1;
        },

        addClass: function(el, className) {
            if (el.className === '') {
                el.className = className;
            }else if (el.className !== '' && !this.hasClass(el, className)) {
                el.className = el.className + ' ' + className;
            }
        },

        removeClass: function(el, className) {
            if (this.hasClass(el, className)) {
                el.className = (' ' + el.className + ' ').replace(' ' + className + ' ', ' ').replace(/^ | $/g,'');
            }
        },

        getElementsByClassName: function(className, tag, root) {
            if (!root) {return [];}
            var nodes = [],
                elements = root.getElementsByTagName(tag);

            for (var i = 0, len = elements.length; i < len; i++) {
                if ( this.hasClass(elements[i], className) ) {
                    nodes[nodes.length] = elements[i];
                }
            }

            return nodes;
        },

        getPreviousSibling: function(node) {
            while (node) {
                node = node.previousSibling;
                if ( node && node.nodeType == 1 ) {
                    return node;
                }
            }
            return null;
        },

        getNextSibling: function(node) {
            while (node) {
                node = node.nextSibling;
                if ( node && node.nodeType == 1 ) {
                    return node;
                }
            }
            return null;
        },

        getChildren: function(node) {	//get node's direct children
            var nodes = node.children || node.childNodes, i, iLen, arrNew = [];
            iLen = nodes.length;
            if (iLen === 0) {return arrNew;}
            if (node.children) {
                return nodes;
            }else {
                for (i=0;i<iLen;i++) {
                    //delete text node and not direct childs
                    if (nodes[i] && (nodes[i].nodeType == 3 || (nodes[i].nodeType == 1 && nodes[i].parentNode != node))) {
                        continue;
                    }
                    arrNew[arrNew.length] = nodes[i];
                }
            }

            return arrNew;
        },

        getParent: function(node, tag, cls) {    //get node's first parent or grant parent node by tag
            var dParent = node;
            do {
                dParent = dParent.parentNode;
            }while(dParent &&
                (
                    ( typeof(cls) == 'undefined' && dParent.tagName.toLowerCase() != tag) ||
                    ( typeof(cls) != 'undefined' && (!this.hasClass(dParent, cls) || dParent.tagName.toLowerCase() != tag) )
                ) &&
                dParent.tagName.toLowerCase() != 'body'
            );

            return dParent && (
                    ( typeof(cls) == 'undefined' && dParent.tagName.toLowerCase() == tag) ||
                    ( typeof(cls) != 'undefined' && this.hasClass(dParent, cls) && dParent.tagName.toLowerCase() == tag )
                ) ? dParent : null;
        },

        getPosition: function() {   //取元素坐标，如元素或其上层元素设置position relative，应该getpos(子元素).y-getpos(父元素).y from easyUI.org.cn
            return document.documentElement.getBoundingClientRect && function(o){//IE,FF,Opera
                var pos = o.getBoundingClientRect(), root = o.ownerDocument || o.document;
                return {left:pos.left+root.documentElement.scrollLeft,top:pos.top+root.documentElement.scrollTop};
            } || function(o){//Safari,Chrome,Netscape,Mozilla
                var x = 0, y = 0;
                do{x += o.offsetLeft;y += o.offsetTop;}while((o=o.offsetParent));
                return {left:x,top:y};
            };
        }(),

        isChild: function(sunObj, parentObj) {  //--check if the parent obj is sun or grandsun of the parent obj
            if (!sunObj || !parentObj) {return 'we need sun element object and parent element object.';}

            if (parentObj.tagName && parentObj.tagName.toLowerCase() == 'body') {
                return true;
            }
            while (sunObj && sunObj.tagName && sunObj.tagName.toLowerCase() != 'body') {
                if (sunObj.parentNode == parentObj) {
                    return true;
                }
                sunObj = sunObj.parentNode;
            }
            return false;
        },

        getStyle: function(dom,stylename,bNum) { //获得元素当前样式 from easyUI.org.cn
            if(dom.currentStyle){
                return bNum ? parseFloat(dom.currentStyle[stylename].replace(/px|pt|em/ig,'')) : dom.currentStyle[stylename];
            }else{
                return bNum ? parseFloat(window.getComputedStyle(dom,null).getPropertyValue(stylename).replace(/px|pt|em/ig,'')) : window.getComputedStyle(dom,null).getPropertyValue(stylename);
            }
        }
    },  /*--dom}}--*/

    event: {    /*--event{{--*/
        stopEvent: function(ev) {
           this.stopPropagation(ev);
           this.preventDefault(ev);
        },

        stopPropagation: function(ev) {
           if (ev.stopPropagation) {
               ev.stopPropagation();
           } else {
               ev.cancelBubble = true;
           }
        },

        preventDefault: function(ev) {
           if (ev.preventDefault) {
               ev.preventDefault();
           } else {
               ev.returnValue = false;
           }
        },

        getEvent: function(e, boundEl) {
           var ev = e || window.event;

           if (!ev) {
               var c = this.getEvent.caller;
               while (c) {
                   ev = c.arguments[0];
                   if (ev && Event == ev.constructor) {
                       break;
                   }
                   c = c.caller;
               }
           }

           return ev;
        },

        getTarget: function(ev, resolveTextNode) {
            var t = ev.target || ev.srcElement;
            return this.resolveTextNode(t);
        },

        getRelatedTarget: function(ev) {
            var t = ev.relatedTarget;
            if (!t) {
                if (ev.type == "mouseout") {
                    t = ev.toElement;
                } else if (ev.type == "mouseover") {
                    t = ev.fromElement;
                }
            }

            return this.resolveTextNode(t);
        },

        resolveTextNode: function(n) {
            try {
                if (n && 3 == n.nodeType) {
                    return n.parentNode;
                }
            } catch(e) {}

            return n;
        },

        getXY: function(e) {    //get event position
            return {x:e.pageX ? e.pageX : e.clientX + document.documentElement.scrollLeft, y:e.pageY ? e.pageY : e.clientY + document.documentElement.scrollTop};
        },

        on: function () {
            if (window.addEventListener) {
               return function(el, sType, fn, capture) {
                   el.addEventListener(sType, fn, (capture));
               };
            } else if (window.attachEvent) {
               return function(el, sType, fn, capture) {
                   el.attachEvent("on" + sType, fn);
               };
            } else {
               return function(){};
            }
        }()
    },  /*--event}}--*/

    connection: {   /*--connection{{--*/
        ajax: { //create and manage xhr object
            xhr: [],
            activeRequestCount: 0,

            Try: {  //from prototypejs.org
                these: function() {
                    var returnValue;

                    for (var i = 0, length = arguments.length; i < length; i++) {
                        var lambda = arguments[i];
                        try {
                            returnValue = lambda();
                            break;
                        } catch (e) {}
                    }

                    return returnValue;
                }
            },

            getTransport: function() {  //from prototypejs.org
                return this.Try.these(
                    function(){return new XMLHttpRequest();},
                    function(){return new ActiveXObject("Msxml2.XMLHTTP");},
                    function(){return new ActiveXObject("Microsoft.XMLHTTP");}
                ) || false;
            },

            getXHR: function(){ //get xmlHttpRequest object
                for(var i=0; i<this.xhr.length; i++){
                    if (this.xhr[i].readyState == 0 || this.xhr[i].readyState == 4){
                        if (this.xhr[i].readyState == 4) {this.xhr[i].abort();}
                        return this.xhr[i];
                    }
                }

                this.xhr[this.xhr.length] = this.getTransport();
                return this.xhr[this.xhr.length - 1];
            }
        },

        asyncRequest: function(method, uri, callback, postData) {   //get or post ajax request and not support file upload
            if(typeof(callback)=='undefined'){var callback={};}
            var args = typeof(postData) == 'string' ? postData : '';    //arguments
            if (typeof(postData) == 'object') {
                for (key in postData) {
                    args += key + '=' + postData[key] + '&';
                }
            }
            args = args.replace(/&$/,'');

            var bAsync = typeof(callback.async) != 'undefined' ? callback.async : true;
            var arr = [],i=0;
            var xhr = this.ajax.getXHR();
			if(!xhr){return;}
            
            var fnCallback = function(fn) {
                if (typeof(callback[fn]) == 'undefined') {return false;}
                if (callback.argument && callback.argument.length > 0) {
                    arr[0] = xhr;
                    for (i=0;i<callback.argument.length;i++) {
                        arr[i+1] = callback.argument[i];
                    }
                    callback[fn].apply(xhr,arr);
                }else {
                    callback[fn](xhr);
                }
            };

            xhr.onreadystatechange = function(){
                if(xhr.readyState == 4){
                    if(xhr.status == 200){
                        fnCallback('success');
                    }else{
                        fnCallback('failure');
                    }
                }
            };

            if (!bAsync && (/Firefox/ig).test(navigator.userAgent)) {  //if is firefox and no async
                xhr.onload = function() {
                    fnCallback('success');
                };
            }

            if (!callback.cache) {  //if not cache
                uri += ((uri.indexOf('?') == -1)?'?':'&') + "rnd=" + new Date().valueOf().toString();
            }

            if (method.toUpperCase() == 'POST') {   //send request
                xhr.open('POST', encodeURI(uri), bAsync);
                xhr.setRequestHeader('Content-Length',args.length);
                xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                xhr.send(args);
            }else if (method.toUpperCase() == 'GET') {
                uri += ((uri.indexOf('?') == -1)?'?':'&') + args;
                xhr.open('GET', encodeURI(uri), bAsync);
                xhr.setRequestHeader('Content-Length',args.length);
                xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                xhr.send(null);
            }
        },

        xRequest:function(url,id){  //取跨域数据，script标签id，目标url from easyUi.org.cn
            var sId = id||'oscript'+Math.random().toString().replace(/\./g,'');
			var oScript = document.getElementById(sId);
			var head = document.getElementsByTagName("head").item(0);
			if (oScript){head.removeChild(oScript);}
			oScript = document.createElement("script");
			oScript.setAttribute("src", url);
			oScript.setAttribute("id",sId);
			oScript.setAttribute("type","text/javascript");
			oScript.setAttribute("language","javascript");
			head.appendChild(oScript);
			return oScript;
		}
    },/*--connection}}--*/

    clone: function(data) { //clone an object or an array
        if (typeof(data) != 'object') {return null;}
        var obj = {};
        if (!data.length) {
            for (key in data) {
                obj[key] = data[key];
            }
        }else {
            obj = [];
            for (var i=0;i<data.length;i++) {
                obj[i] = data[i];
            }
        }

        return obj;
    },

    doWhileExist: function(elId, fun){  //init function
        var argu = Array.prototype.slice.call(arguments,2);
        var module = document.getElementById(elId);
        if (module) {
            argu.unshift(module);
            fun.apply(null, argu);
        }
    }
};
